package com.sos.adapter;

import java.util.ArrayList;

import com.sos.R;
import com.sos.vo.Contactor;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


/****
 * 系统联系人适配器
 * **/
public class SysContactorAdapter extends ArrayAdapter<Contactor>{
	private final String TAG = SysContactorAdapter.class.getSimpleName();
	
	private LayoutInflater inflater;
	protected ArrayList<Contactor> items;
	
	/**
	 * 构造函数
	 * @param context
	 * @param layout
	 * @param items
	 */
    public SysContactorAdapter(Context context, int layout, ArrayList<Contactor> items) {
        super(context, layout, items);
        inflater = LayoutInflater.from(context);
        this.items = items;
    }
    
    @Override
    public int getCount() {
        return items.size();
    }
    
    @Override
    public synchronized void clear() {
        items.clear();
        notifyDataSetChanged();
    }
    
    @Override
    public synchronized void add(Contactor bean) {
    	items.add(bean);
    }
    
	public synchronized void modify(int index, Contactor bean){
		items.set(index, bean);
	}
	
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
    	Log.i(TAG, "getView");
    	ViewHolder holder;

        if (convertView == null) {
        	convertView = inflater.inflate(R.layout.sys_contactor_list_item, null);
        	
        	holder = new ViewHolder();
        	holder.phone = (TextView) convertView.findViewById(R.id.phone);
        	holder.name = (TextView) convertView.findViewById(R.id.name);
        	convertView.setTag(holder);
        }else{
        	holder = (ViewHolder) convertView.getTag();
        }
        
        Contactor bean = items.get(position);
        if (bean != null) {
        	holder.name.setText(bean.getName());        	
        	holder.phone.setText(bean.getPhone());
        }
        return convertView;
    }
    
    private static class ViewHolder{
    	TextView name;
    	TextView phone;
    }
}
